package cn.signalpha.modules.shop.controller.api;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.dto.IdDTO;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.modules.shop.dto.OrderRefundDTO;
import cn.signalpha.modules.shop.entity.OrderRefund;
import cn.signalpha.modules.shop.service.OrderRefundService;
import cn.signalpha.modules.shop.vo.OrderRefundVO;
import cn.signalpha.modules.shop.wrapper.OrderRefundWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/api/order/refund")
@Api(value = "订单退换货", tags = "订单退换货")
public class ApiOrderRefundController extends BaseController {

	private final OrderRefundService orderRefundService;

    @GetMapping("/page")
    @ApiOperation(value = "分页", notes = "")
    @ApiImplicitParams({
        @ApiImplicitParam(name="orderNo", dataType = "string", value = "订单号"),
		@ApiImplicitParam(name="refundNo", dataType = "string", value = "退款单号"),
		@ApiImplicitParam(name="orderType", dataType = "string", value = "订单类型"),
    })
    public R<IPage<OrderRefundVO>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		params.put("userId", getUser().getUserId());
		QueryWrapper<OrderRefund> wrapper = orderRefundService.selectOrderRefundWrapper(params);
		IPage<OrderRefund> page = orderRefundService.page(Condition.getPage(query), wrapper);
		return R.data(OrderRefundWrapper.build().pageVO(page));
    }

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<OrderRefundVO> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(OrderRefundWrapper.build().entityVO(orderRefundService.getById(id)));
	}

	@PostMapping("/apply")
	@ApiOperation(value = "申请售后", notes = "传入orderRefund")
	public R apply(@Valid @RequestBody OrderRefundDTO.ApplyDTO applyDTO) {
		orderRefundService.buyerApply(applyDTO, true);
		return R.success("申请已提交，请耐心等待审核");
	}

	@PostMapping("/cancel")
	@ApiOperation(value = "取消售后", notes = "")
	public R cancel(@Valid @RequestBody IdDTO idDTO) {
		orderRefundService.buyerCancel(idDTO.getId());
		return R.success("取消成功");
	}

	@PostMapping("/shipping/save")
	@ApiOperation(value = "填写物流", notes = "")
	public R shippingSave(@Valid @RequestBody OrderRefundDTO.LogisticsDTO logisticsDTO) {
		return R.status(orderRefundService.buyerLogisticsSave(logisticsDTO));
	}

	@PostMapping("/shipping/update")
	@ApiOperation(value = "修改物流", notes = "")
	public R shippingUpdate(@Valid @RequestBody OrderRefundDTO.LogisticsDTO logisticsDTO) {
		return R.status(orderRefundService.buyerLogisticsUpdate(logisticsDTO));
	}

}
